package com.mall.util;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.mall.service.CommodityService;
@Service
public class Seckill implements ApplicationListener<ContextRefreshedEvent> {
	public static ThreadPoolExecutor tpe;
	private static int num=0;
	@Autowired
	private SeckillThread seckillThread;
	@Autowired
	private CommodityService commoditeService;
	public static ThreadPoolExecutor getThreadExecutor() {
		return tpe;
	}
	/**
	 * 容器启动时执行
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
			if(num==0){
				tpe = new ThreadPoolExecutor(30,60,13000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(30),
						new ThreadPoolExecutor.AbortPolicy());
				tpe.execute(seckillThread);
				try {
					commoditeService.commListToRedis();
				} catch (Exception e) {
					e.printStackTrace();
				}
				}
				num=num+1;
			}
			/**
			 * 参数分别表示,最小线程数,最大线程数,最大休眠时间,休眠时间单位(常量毫秒),线程列队,异常处理
			 */
			
		
}
